package org.jaaksi.demo.animator;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.TextView;

/**
 * @author ccx
 **/
@SuppressLint("NewApi")
public class MyAnimationUtils {
    public static ObjectAnimator getNumberAnimation(
        final int from, final int end, int duration, final TextView tv) {
        String text = tv.getText().toString();
        int start = 0;
        if (!TextUtils.isEmpty(text)) {
            start = Integer.parseInt(text);
        }

        ObjectAnimator numberAnimation = ObjectAnimator.ofInt(tv, "numbincrease", start,
            end);
        int reduration = (int) (((double) (end - start) / end) * duration);
        numberAnimation.addListener(new Animator.AnimatorListener() {

            @Override
            public void onAnimationStart(Animator arg0) {
            }

            @Override
            public void onAnimationRepeat(Animator arg0) {

            }

            @Override
            public void onAnimationEnd(Animator arg0) {
            }

            @Override
            public void onAnimationCancel(Animator arg0) {
            }
        });
        numberAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (Integer) animation.getAnimatedValue();
                Spannable word = new SpannableString(String.valueOf(value));
                word.setSpan(new AbsoluteSizeSpan(35, true), 0, word.length(),
                    Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                word.setSpan(new StyleSpan(Typeface.BOLD), 0, word.length(),
                    Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                tv.setText(word);
            }
        });
        numberAnimation.setDuration(reduration);
        return numberAnimation;
    }

    public static void fadeInAndoutAnimation(View view, boolean show, int pivotX) {
        float arg1;
        float arg2;
        if (show) {
            arg1 = 0;
            arg2 = 1f;
            view.setPivotX(pivotX);
            view.setPivotY(0);
        } else {
            arg1 = 1f;
            arg2 = 0;
        }

        // PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha",
        // arg1,
        // arg2);
        PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX",
            arg1, arg2);
        PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY",
            arg1, arg2);

        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view,
            pvhY, pvhZ).setDuration(500);
        animator.setInterpolator(new AccelerateInterpolator());
        animator.start();
    }
}
